package edu.guigu.servicedemo;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

/**
* 前台服务
* @author Yuansir
* @since 2025/9/5 9:56
* @version V1.0
*/
public class MyForegroundService extends Service {

    public MyForegroundService() { }

    @Override
    public void onCreate() {
        super.onCreate();
        initDefaultChannels();
        createNotification();
        // TODO： 升级为前台服务,获得更高的优先级，不呗系统轻易终止
        startForeground(1, notification);
    }

    private Thread thread;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 模拟服务的后台工作
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    Log.d("TAG", "前台服务 runing~~~~~~~~: ");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        });
        thread.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("TAG", "onDestroy: ~");
        if(thread != null){
            thread.interrupt();
            thread = null;
        }
        // TODO: "降级"为普通服务
        // STOP_FOREGROUND_REMOVE 停止前台服务并移除通知
        // STOP_FOREGROUND_DETACH 停止前台服务但保留通知
        stopForeground(Service.STOP_FOREGROUND_REMOVE);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private final String CHANNEL_HIGH = "gg_channel_high";
    private final String CHANNEL_NORMAL = "gg_channel_normal";
    private final String CHANNEL_SILENT = "gg_channel_silent";
    private NotificationManager notificationManager;
    private Notification notification;

    private void initDefaultChannels(){
        createHighChannel();
        createNormalChannel();
        createSilentChannel();
    }

    private void createHighChannel(){
        if(notificationManager == null) {
            notificationManager = getSystemService(NotificationManager.class);
        }
        // 检测 此渠道 是否已存在了
        if(notificationManager.getNotificationChannel(CHANNEL_HIGH) != null){
            return;
        }
        // 准备渠道对象
        NotificationChannel channel_high = new NotificationChannel(CHANNEL_HIGH,"重要型通知", NotificationManager.IMPORTANCE_HIGH);
        channel_high.setLightColor(Color.RED); // 指示灯
        channel_high.setDescription("这是一个用来展示重要通知的渠道");
        channel_high.enableVibration(true);
        channel_high.setVibrationPattern(new long[]{100,200,100,200});
        channel_high.setShowBadge(true); // 是否显示角标
        notificationManager.createNotificationChannel(channel_high);
    }

    private void createNormalChannel(){
        if(notificationManager == null) {
            notificationManager = getSystemService(NotificationManager.class);
        }
        // 检测 此渠道 是否已存在了
        if(notificationManager.getNotificationChannel(CHANNEL_NORMAL) != null){
            return;
        }
        // 准备渠道对象
        NotificationChannel channel = new NotificationChannel(CHANNEL_NORMAL,"普通型通知", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setLightColor(Color.RED); // 指示灯
        channel.setDescription("这是一个用来展示常规通知的渠道");
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{100,100});
        channel.setShowBadge(true); // 是否显示角标
        notificationManager.createNotificationChannel(channel);
    }

    private void createSilentChannel(){
        if(notificationManager == null) {
            notificationManager = getSystemService(NotificationManager.class);
        }
        // 检测 此渠道 是否已存在了
        if(notificationManager.getNotificationChannel(CHANNEL_SILENT) != null){
            return;
        }
        // 准备渠道对象
        NotificationChannel channel = new NotificationChannel(CHANNEL_SILENT,"静默型通知", NotificationManager.IMPORTANCE_LOW);
        channel.setDescription("这是一个用来展示静默通知的渠道");
        notificationManager.createNotificationChannel(channel);
    }

    private void createNotification(){
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent =  PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_IMMUTABLE);

        notification = new Notification.Builder(this, CHANNEL_NORMAL)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("GG音乐")
                .setContentText("GG音乐播放中~~~~~~")
                .setContentIntent(pendingIntent)
                .build();
    }
}